package com.xingkeduo.business.user.service;

import com.xingkeduo.dao.database.DatabaseAccessObject;
import com.xingkeduo.pojo.query.CraftsmanJobHistoryQO;
import com.xingkeduo.pojo.query.CraftsmanJobQO;
import com.xingkeduo.pojo.table.CraftsmanJobHistoryTO;
import com.xingkeduo.pojo.table.CraftsmanJobTO;
import com.xingkeduo.util.pojo.enums.HistoryTypeEnum;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.IdentityUtil;
import com.xingkeduo.util.tool.IdentityUtil.Worker;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 一线员工工作属性类
 *
 * @author 高杰
 * @time 2018年12月27日 上午11:45:44
 * @description
 */
@Component
public class CraftsmanJobService {
    private static final String NAMESPACE = CommonUtil.formatNamespace(CraftsmanJobTO.class);
    private static final String NAMESPACE_HISTORY = CommonUtil.formatNamespace(CraftsmanJobHistoryTO.class);
    @Resource(name = "mysqlUserMasterDao")
    private DatabaseAccessObject<CraftsmanJobTO, CraftsmanJobQO> dao;
    @Resource(name = "mysqlUserMasterDao")
    private DatabaseAccessObject<CraftsmanJobHistoryTO, CraftsmanJobHistoryQO> historyDao;

    /**
     * 查询
     *
     * @param code
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月27日 上午11:45:57
     */
    public CraftsmanJobTO selectByPrimary(long id) throws Throwable {
        return dao.select(NAMESPACE, id);
    }

    /**
     * 查询主键
     *
     * @param query
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月8日 下午2:13:33
     */
    public List<Long> selectPrimatyByQuery(CraftsmanJobQO query) throws Throwable {
        return dao.selectIndex(NAMESPACE, query);
    }

    /**
     * @param nickName:
     * @return : java.util.List<java.lang.Long>
     * @author : gaozifeng
     * @date : 2019/5/18 13:48
     */
    public List<Long> selectByNickName(String nickName) throws Throwable {
        CraftsmanJobQO query = new CraftsmanJobQO().setNickName(nickName);
        return dao.selectIndex(NAMESPACE, query);
    }


    /**
     * @param query:
     * @return : java.util.List<java.lang.Long>
     * @author : gaozifeng
     * @date : 2019/3/30 13:48
     */
    public List<CraftsmanJobTO> selectByQueryForShopId(CraftsmanJobQO query) throws Throwable {
        return dao.select(NAMESPACE, query);
    }


    /**
     * 添加
     *
     * @param craftsmanJob
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月27日 上午11:46:03
     */
    @Transactional
    public void insert(CraftsmanJobTO craftsmanJob) throws Throwable {
        if (dao.insert(NAMESPACE, craftsmanJob) < 1)
            throw new DataException(DataException.Type.FAIL, "一线员工工作信息");

        CraftsmanJobHistoryTO history = new CraftsmanJobHistoryTO().setHistoryId(IdentityUtil.nextId(Worker.USER))
                .setHistoryType(HistoryTypeEnum.NEW)
                .setCraftsmanJob(craftsmanJob.setIsDelete(YesNoEnum.NO).setVersion(1))
                .setCreatedBy(craftsmanJob.getCreatedBy()).setUpdatedBy(craftsmanJob.getUpdatedBy());
        historyDao.insert(NAMESPACE_HISTORY, history);
    }

    /**
     * 修改
     *
     * @param craftsmanJob
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月27日 上午11:46:08
     */
    @Transactional
    public void update(CraftsmanJobTO craftsmanJob) throws Throwable {
        if (dao.update(NAMESPACE, craftsmanJob) < 1)
            throw new DataException(DataException.Type.FAIL, "一线员工工作信息");

        craftsmanJob = dao.select(NAMESPACE, craftsmanJob.getId());

        CraftsmanJobHistoryTO history = new CraftsmanJobHistoryTO().setHistoryId(IdentityUtil.nextId(Worker.USER))
                .setHistoryType(HistoryTypeEnum.MODIFY).setCraftsmanJob(craftsmanJob)
                .setCreatedBy(craftsmanJob.getUpdatedBy()).setUpdatedBy(craftsmanJob.getUpdatedBy());
        historyDao.insert(NAMESPACE_HISTORY, history);
    }

    /**
     * 统计
     *
     * @param craftsmanJobQO:
     * @return : long
     * @author : gaozifeng
     * @date : 2019/6/18 14:56
     */
    public long count(CraftsmanJobQO craftsmanJobQO) throws Throwable {
        return dao.selectCount(NAMESPACE, craftsmanJobQO);
    }
}
